namespace Plugins.Serializer.Internal
{
    using System;
    using System.Globalization;
    using Plugins.Interfaces;
    using Plugins.Library;

    internal static class PluginRuntimeHelper
    {
        internal static IPluginFormatAttributesInternal GetFormatAttributes(IPluginRuntimeSerializerInternal serializer)
        {
            return serializer.InternalAttributes;
        }

        internal static IPluginRuntimeSerializerInternal GetSerializer(IPluginTypeFormatter formatter)
        {
            IPluginTypeFormatterInternal _formatter = formatter as IPluginTypeFormatterInternal;
            if (_formatter != null)
            {
                IPluginRuntimeSerializerInternal _serializer = _formatter.InternalSerializer;
                if (_serializer != null)
                {
                    return _serializer;
                }
            }
            return null;
        }

        internal static IPluginRuntimeSerializerInternal GetSerializer(Type type, IPluginTypeFormatter formatter)
        {
            IPluginTypeFormatterInternal _formatter = formatter as IPluginTypeFormatterInternal;
            if (_formatter != null)
            {
                IPluginRuntimeSerializerInternal _serializer = _formatter.InternalSerializer;
                if (_serializer != null)
                {
                    if (!PluginTypeHelper.IsCompatibleType(type, _serializer.RuntimeType))
                    {
                        throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Type {0} is not compatible with type {1}", type, _serializer.RuntimeType));
                    }
                    return _serializer;
                }
            }
            return null;
        }
    }
}

